package com.android.lovingpet.adopt.host;

import android.os.Bundle;
import android.support.annotation.Nullable;

import com.android.lovingpet.base.BasePresenter;
import com.android.lovingpet.bean.Host;
import com.android.lovingpet.common.Back;
import com.android.lovingpet.common.FileModel;
import com.android.lovingpet.mine.pet.PetModel;
import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by ikata on 2018/4/15.
 */

public class HostPresenter extends BasePresenter<IHostContract.View> implements IHostContract.Presenter{
    private HostModel hostModel;
    private PetModel petModel;
    public HostPresenter()
    {
        hostModel=new HostModel();
        petModel=new PetModel();
    }

    private void sort(HashMap<String,String> params)
    {
        hostModel.sort(params, new Back() {
            @Override
            public void onFailure(String msg) {
                if (getView()!=null)
                    getView().showToast(msg);
                    getView().next();
            }
            @Override
            public void onSuccess(@Nullable Bundle bundle) {
                ArrayList<Integer> ids=bundle.getIntegerArrayList("ids");
                //判断是获取更多host还是刷新host列表(offset是指从服务端获取的host列表的第几个数据)
                if (Integer.valueOf(params.get("offset"))==1) {
                    if (ids!=null) {
                        for (Integer id : ids) {
                            getHostInfo(id);
                        }
                    }
                }
                getView().next();
            }
        });
    }
    @Override
    public void sortByAddress(String address, int offset, int num) {
        HashMap<String,String> params=new HashMap<>();
        params.put("address",address);
        params.put("offset", String.valueOf(offset));
        params.put("num", String.valueOf(num));
        sort(params);
    }
    @Override
    public void sortByCredit(int offset, int num) {
        HashMap<String,String> params=new HashMap<>();
        params.put("credit", String.valueOf(1));
        params.put("offset", String.valueOf(offset));
        params.put("num", String.valueOf(num));
        sort(params);
    }




    @Override
    public void loadHead(int id) {
        FileModel.getInstance().downloadImage(id, 3, new FileModel.FileBack() {
            @Override
            public void loadSuccess(String url) {

            }

            @Override
            public void onFailure(String msg) {

            }
            @Override
            public void onSuccess(@Nullable Bundle bundle) {

            }
        });
    }

    @Override
    public void getAllHost(int offset,int num) {
        HashMap<String,String> params=new HashMap<>();
        params.put("offset", String.valueOf(offset));
        params.put("num", String.valueOf(num));
        hostModel.sortByTime(params,new Back() {
            @Override
            public void onFailure(String msg) {
                if (getView()==null)return;
                getView().showToast(msg);
            }
            @Override
            public void onSuccess(@Nullable Bundle bundle) {
                if (bundle == null) {
                    return;
                }
                ArrayList<Integer> ids=bundle.getIntegerArrayList("ids");
                for (Integer id : ids) {
                   getHostInfo(id);
                }
            }
        });
    }

    public void getHostInfo(int id)
    {
        HashMap<String,String> params=new HashMap<>();
        params.put("id", String.valueOf(id));
        hostModel.getHostInfo(params, new Back() {
            @Override
            public void onFailure(String msg) {
                if (getView() == null) {
                    return;
                }
                getView().showToast(msg);
                getView().next();
            }
            @Override
            public void onSuccess(@Nullable Bundle bundle) {
                assert bundle != null;
                Host host =bundle.getParcelable("result");
                FileModel.getInstance().downloadImage(host.getId(), 3, new FileModel.FileBack() {
                    @Override
                    public void loadSuccess(String url) {
                        host.setImage(url);
                        if (getView()==null) {
                            return;
                        }
                        getView().addHost(host);
                    }

                    @Override
                    public void onFailure(String msg) {

                    }

                    @Override
                    public void onSuccess(@Nullable Bundle bundle) {

                    }
                });

            }
        });

    }
}
